package com.greet.service.impl;

import com.greet.context.BaseContext;
import com.greet.dto.DishDTO;
import com.greet.dto.DishPageQueryDTO;
import com.greet.entity.Category;
import com.greet.entity.Dish;
import com.greet.entity.DishFlavor;
import com.greet.mapper.CategoryMapper;
import com.greet.mapper.DishFlavorMapper;
import com.greet.mapper.DishMapper;
import com.greet.result.PageResult;
import com.greet.service.DishService;
import com.greet.utils.PojoTypeTranformationUtil;
import com.greet.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @Override
    public PageResult dishPageQuery(DishPageQueryDTO dishPageQueryDTO) {
        Integer index = (dishPageQueryDTO.getPage() - 1) * dishPageQueryDTO.getPageSize();
        dishPageQueryDTO.setPage(index);
        List<DishVO> dishList = dishMapper.selectPageQuery(dishPageQueryDTO);
        return new  PageResult(dishMapper.countAll() ,dishList);
    }

    @Override
    public void setStatus(Integer status, Long id) {
        dishMapper.update(Dish.builder().status(status).id(id).build());
    }


    @Override
    public DishVO getById(Long id) {
        //查询菜品信息
        List<Dish> dishList = dishMapper.select(Dish.builder().id(id).build());
        Dish resultDish;
        if(!dishList.isEmpty())resultDish = dishList.get(0);
        else return null;
        //查询菜品相关分类信息
        List<Category> categoryList = categoryMapper.select(
                Category.builder().id(resultDish.getCategoryId()).build()
        );
        //查询菜品关联口味信息
        List<DishFlavor> flavors = dishFlavorMapper.select(
                DishFlavor.builder().dishId(resultDish.getId()).build()
        );

        DishVO dishVO = new DishVO(resultDish.getId(),
                resultDish.getName(), resultDish.getCategoryId(),
                resultDish.getPrice(), resultDish.getImage(),
                resultDish.getDescription(), resultDish.getStatus(),
                resultDish.getUpdateTime(), categoryList.get(0).getName(),
                flavors);
        return dishVO;
    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        List<Dish> result = dishMapper.select(Dish.builder().categoryId(categoryId).build());
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertDish(DishDTO dishDTO){
        //插入菜品
        Dish dish = PojoTypeTranformationUtil.toDish(dishDTO);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setCreateTime(LocalDateTime.now());
        dish.setId(null);
        dishMapper.insert(dish);
        //获取插入的dish对象
        Dish updateRes = dishMapper.selectByName(dish.getName());

        //获取口味信息
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        //插入口味信息
        for (DishFlavor dishFlavor : dishFlavorList){
            dishFlavor.setDishId(updateRes.getId());
            dishFlavorMapper.insert(dishFlavor);
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDish(DishDTO dishDTO) {
        //更新dish表
        Dish dish = PojoTypeTranformationUtil.toDish(dishDTO);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.update(dish);
        //更新dish_flavor
        // 1,删除所有与菜品有关的口味，
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        // 2,重新插入
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        for (DishFlavor dishFlavor : dishFlavorList){
            dishFlavor.setDishId(dish.getId());
            dishFlavorMapper.insert(dishFlavor);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(Long[] ids) {
        dishMapper.deletByIds(Arrays.stream(ids).toList());
        dishFlavorMapper.deleteByDishIds(Arrays.stream(ids).toList());
    }


}
